package beanstao.util.j2ee.request;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.HashMap;

import net.sf.json.JSONArray;

import beanstao.util.Encoding;
import beanstao.util.Sber;
import beanstao.util.enums.operator.Comparison;
import beanstao.util.id.Id;
import beanstao.util.log.ILog;
import beanstao.util.log.LogHelper;
import beanstao.util.store.IQD;
import beanstao.util.store.KV;
import beanstao.util.store.database.Where;
import beanstao.util.store.domainobject.fields.PublicField;


public abstract class AbstractQD implements IQD, ILog
{

	protected HashMap<String, KV> _KeyValueMap = new HashMap<String, KV>();
	protected Id _ModifyId;
	protected String _QueryType;

	/**
	 * 对参数进行解码操作
	 */
	protected String decodeParam(final String s)
	{
		String result = s;

		try
		{
			result = URLDecoder.decode(result, Encoding.UTF8);
		}
		catch (UnsupportedEncodingException e)
		{
			this.logError(e);
		}

		return result;
	}

	/**
	 * 根据指定的键获取值
	 * 
	 * @param key
	 * @return
	 */
	@Override
	public KV get(final String key)
	{
		return _KeyValueMap.get(key.toLowerCase());
	}

	public void put(String key, KV value)
	{
		_KeyValueMap.put(key, value);
	}

	/**
	 * 根据Key删除
	 * 
	 * @param key
	 * @return
	 */

	public KV remove(String key)
	{
		return _KeyValueMap.remove(key);
	}

	/**
	 * 获得数据集合的键的集合
	 * 
	 * @return
	 */
	@Override
	public Collection<String> getKeys()
	{
		return _KeyValueMap.keySet();
	}

	public Id getModifyId()
	{
		return _ModifyId;
	}

	/**
	 * 获取针对实体的查询类型
	 * 
	 * @return
	 */
	public String getQueryType()
	{
		return _QueryType;
	}

	/**
	 * 获得数据集合的值的集合
	 * 
	 * @return
	 */
	@Override
	public Collection<KV> getValues()
	{
		return _KeyValueMap.values();
	}

	@Override
	public String toXPath()
	{
		Sber xpath = Sber.ME("//").append(this.getQueryType().toLowerCase());
		for (String key : this.getKeys())
		{
			if (PublicField.MODIFIOR.equals(key))
				continue;
			KV kv = this.get(key);
			if ((kv.$Key() == null) || (kv.$Key() == ""))
				continue;
			Sber c = Sber.ME();
			c.append("[@");
			c.append(kv.$Key());
			c.append(Comparison.toString(kv.$CO()));
			c.append("'");
			c.append(kv.$V());
			c.append("']");
			xpath.append(c);
		}
		return xpath.toString();

	}

	/**
	 * 将本类型的数据组成一般的Sql的Where语句
	 * 
	 * @see beanstao.util.IQD.servlet.IRequestData#toSimpleWhere(java.util.ArrayList, java.util.ArrayList)
	 */
	@Override
	public Where toSimpleWhere()
	{
		return this.toSimpleWhere(true);
	}

	/**
	 * 将本类型的数据组成一般的Sql的Where语句
	 * 
	 * @see beanstao.util.IQD.servlet.IRequestData#toSimpleWhere(java.util.ArrayList, java.util.ArrayList)
	 */
	@Override
	public Where toSimpleWhere(final boolean isContainModiftor)
	{
		Where where = Where.ME();
		for (KV kv : _KeyValueMap.values())
		{
			if (kv.isEmpty())
				continue;
			if (kv.$Key().equals("top") || kv.$Key().equals("modifior") || kv.$Key().equals("groupby") || kv.$Key().equals("desc") || kv.$Key().equals("asc") || kv.$Key().equals("start") || kv.$Key().equals("limit") || kv.$Key().equals("columns"))
				continue;
			if (!isContainModiftor && kv.$Key().equals(PublicField.MODIFIOR))
				continue;
			// 确定查询的条件的集合，当修改时，判断是修改的参数还是条件参数
			if (_KeyValueMap.keySet().contains("wheres"))
			{
				JSONArray list = JSONArray.fromObject(_KeyValueMap.get("wheres").$V());
				if (list.contains(kv.$Key()))
				{
					kv.setWhere(where);
				}
			}
			else
			{
				kv.setWhere(where);
			}
		}
		return where;
	}

	// ------------------------------

	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("RequestData [_KeyValueMap=").append(_KeyValueMap).append(", _QueryType=").append(_QueryType).append("]");
		return builder.toString();
	}

	@Override
	public void logDebug(final Object message)
	{
		LogHelper.logDebug(this, message);
	}

	@Override
	public void logDebug(final Object message, final Throwable t)
	{
		LogHelper.logDebug(this, message, t);
	}

	@Override
	public void logError(final Object message)
	{
		LogHelper.logError(this, message);
	}

	@Override
	public void logError(final Object message, final Throwable t)
	{
		LogHelper.logError(this, message, t);
	}

	@Override
	public void logInfo(final Object message)
	{
		LogHelper.logInfo(this, message);
	}

	@Override
	public void logInfo(final Object message, final Throwable t)
	{
		LogHelper.logInfo(this, message, t);
	}

	@Override
	public void logWarn(final Object message)
	{
		LogHelper.logWarn(this, message);
	}

	@Override
	public void logWarn(final Object message, final Throwable t)
	{
		LogHelper.logWarn(this, message, t);
	}

}
